s = "42"
var myAtoi = function(s) {
    const max=Math.pow(2,31)-1
    const min=-Math.pow(2,31)
    let index=0
    let sign=1
    let num=0
    while(index<s.length&&s[index]===' ')
    {
        index++
    }
    if(index<s.length&&(s[index]==='-'||s[index]==='+'))
    {
        sign=s[index]==='-'?-1:1
        index++
         if(index < s.length && !(s[index] >= '0' && s[index] <= '9')) {
            return 0;
        }
    }
    while(index<s.length&&s[index]>="0"&&s[index]<="9")
    {
        const digit=parseInt(s[index])
        if((num>Math.trunc(max/10))||(num===Math.trunc(max/10)&&digit>max%10))
         {
            return sign===1?max:min
         }
        num=num*10+digit
        index++
    }
    return sign*num
    

    
};
console.log(myAtoi(s));
